package com.hhit.collaborative_learning.learning;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import com.hhit.collaborative_learning.dao.impl.PublicDaoImplMySql;

public class AddFocusServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String userId=request.getParameter("userId");
		String a_userId=request.getParameter("a_userId");
		String type=request.getParameter("type");
		PublicDaoImplMySql dao=new PublicDaoImplMySql();
		if(type.equals("3")){
			response.getWriter().print(dao.addFocus2(Integer.parseInt(userId), a_userId));
			return;
		}
		HttpSession session=request.getSession();
		int myAttention=Integer.parseInt(session.getAttribute("blog_myAttention")+"");
		int attentionEachOther=Integer.parseInt(session.getAttribute("blog_attentionEachOther")+"");
		int attentionMe=Integer.parseInt(session.getAttribute("blog_attentionMe")+"");
		if(dao.addFocus(Integer.parseInt(userId), a_userId)){
			if(type.equals("0")||type.equals("2")){
				myAttention++;
				attentionEachOther++;
				session.setAttribute("blog_myAttention", myAttention);
				session.setAttribute("blog_attentionEachOther", attentionEachOther);
			}else if(type.equals("1")){
				attentionMe++;
				session.setAttribute("blog_attentionMe", attentionMe);
			}
			JSONObject json=new JSONObject();
			json.put("myAttention", myAttention);
			json.put("attentionEachOther", attentionEachOther);
			json.put("attentionMe", attentionMe);
			response.getWriter().print(json.toString());
		}
	}

}
